package com.yd.controller.app;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Users;
import com.yd.pojo.Balance;
import com.yd.pojo.Orders;
import com.yd.pojo.OrdersBeizhu;
import com.yd.pojo.OrdersInfo;
import com.yd.service.BalanceService;
import com.yd.service.OrdersBeizhuService;
import com.yd.service.OrdersInfoService;
import com.yd.service.OrdersService;
import com.yd.tool.UtsBigdeciArith;
import com.yd.tool.UtsReg;

@Controller
public class OrdersApp {
	@Resource
	private OrdersService ordersService;
	@Resource
	private BalanceService balanceService;
	@Resource
	private OrdersInfoService ordersInfoService;
	@Resource
	private OrdersBeizhuService ordersBeizhuService;
	
	//订单list
	@RequestMapping(value = {"/app/orders/list"})
	@ResponseBody
	public String app_orders_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String yundanhao="";
		String page="";
		String size="";
		
		if(UtsReg.getStringKB(request.getParameter("yundanhao"))){yundanhao=request.getParameter("yundanhao").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "1";}
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Orders> list = null;
		EntityWrapper<Orders> ew=new EntityWrapper<Orders>();
		if (yundanhao.length() > 0) {
		OrdersInfo oi = ordersInfoService.selectOne(new EntityWrapper<OrdersInfo>().eq("yundanhao",yundanhao));
		List<OrdersBeizhu> ob = ordersBeizhuService.selectList(new EntityWrapper<OrdersBeizhu>().eq("ddid", yundanhao));
		if(oi==null){
			jsonObject.put("msg", "单号输入错误或无此订单");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		jsonObject.put("data", oi);
		jsonObject.put("data1", ob);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
		}
		list=ordersService.selectList(ew.orderBy("id",false));
		PageInfo<Orders> pages = new PageInfo<Orders>(list);
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	//商户的订单
	@RequestMapping(value = { "/app/orders/merchants/list" })
	@ResponseBody
	public String app_orders_merchants_list(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();
		
		String yundanhao="";
		String token = "";
		String page = "";
		String size = "";
		
		if (UtsReg.getStringKB(request.getParameter("yundanhao"))) {yundanhao = request.getParameter("yundanhao").trim();} 
		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Orders> list = null;
		EntityWrapper<Orders> ew = new EntityWrapper<Orders>();
		if (yundanhao.length() > 0) {
			Orders orders=ordersService.selectOne(ew.eq("yundanhao", yundanhao).eq("merchantid", users_token.getString("id")));
			if(orders==null){jsonObject.put("msg", "单号输入错误或无此订单");jsonObject.put("error", "1");return jsonObject.toString();}
			OrdersInfo oi = ordersInfoService.selectOne(new EntityWrapper<OrdersInfo>().eq("yundanhao",yundanhao));
			List<OrdersBeizhu> ob = ordersBeizhuService.selectList(new EntityWrapper<OrdersBeizhu>().eq("ddid", yundanhao));
			
			jsonObject.put("data", oi);
			jsonObject.put("data1", ob);
			jsonObject.put("error", "0");
			jsonObject.put("msg", "成功");
			return jsonObject.toString();
			}
		ew.eq("merchantid", users_token.getString("id"));
		list = ordersService.selectList(ew.orderBy("id", false));
		PageInfo<Orders> pages = new PageInfo<Orders>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	//客服查看订单
	@RequestMapping(value = {"/app/orders/kefu/list"})
	@ResponseBody
	public String app_orders_kefu_list(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token="";
		String yundanhao="";
		String page="";
		String size="";
		
		if(UtsReg.getStringKB(request.getParameter("token"))){token=request.getParameter("token").trim();}
		if(UtsReg.getStringKB(request.getParameter("yundanhao"))){yundanhao=request.getParameter("yundanhao").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Orders> list = null;
		EntityWrapper<Orders> ew=new EntityWrapper<Orders>();
		if (yundanhao.length() > 0) {
			Orders orders=ordersService.selectOne(ew.eq("yundanhao", yundanhao));
			if(orders==null){
				jsonObject.put("msg", "单号输入错误或无此订单");
				jsonObject.put("error", "1");
				return jsonObject.toString();
			}
			OrdersInfo oi = ordersInfoService.selectOne(new EntityWrapper<OrdersInfo>().eq("yundanhao",yundanhao));
			List<OrdersBeizhu> ob = ordersBeizhuService.selectList(new EntityWrapper<OrdersBeizhu>().eq("ddid", yundanhao));
			
			jsonObject.put("data", oi);
			jsonObject.put("data1", ob);
			jsonObject.put("error", "0");
			jsonObject.put("msg", "成功");
			return jsonObject.toString();
			}
		list=ordersService.selectList(ew.orderBy("id",false));
		PageInfo<Orders> pages = new PageInfo<Orders>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	//商户查看订单状态
	@RequestMapping(value = { "/app/orders/merchants/state/list" })
	@ResponseBody
	public String app_orders_merchants_state_list(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();

		String token = "";
		String state = "";
		String time="";
		String yundanhao="";
		String page = "";
		String size = "";

		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("time"))) {time = request.getParameter("time").trim();}
		if (UtsReg.getStringKB(request.getParameter("yundanhao"))) {yundanhao = request.getParameter("yundanhao").trim();}
		if(UtsReg.getInt(request.getParameter("state"))){state=request.getParameter("state").trim();}
		if (UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Orders> list = null;
		EntityWrapper<Orders> ew = new EntityWrapper<Orders>();
		if(state.length()>0){
			ew.eq("state", state);
		}if(time.length()>0){
			ew.like("addtime", time);
		}
		if (yundanhao.length() > 0) {
			ew.eq("yundanhao", yundanhao);
		}
		ew.eq("merchantid", users_token.getString("id"));
		list = ordersService.selectList(ew.orderBy("id", false));
		if(list==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "没有此数据");return jsonObject.toString();
		}
		PageInfo<Orders> pages = new PageInfo<Orders>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	//商户修改订单状态
	@RequestMapping(value = { "/app/orders/merchants/upState" })
	@ResponseBody
	public String app_orders_merchants_upstate(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();
		//获取参数
		String token = "";
		String yundanhao="";
		String state="";
		String doc="";
		//验证参数
		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("doc"))) {doc = request.getParameter("doc").trim();} else {jsonObject.put("msg", "描述 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getIntXl(request.getParameter("yundanhao"))){yundanhao=request.getParameter("yundanhao").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "单号输入错误或无此订单");return jsonObject.toString();}
		if(UtsReg.getInt(request.getParameter("state"))){state=request.getParameter("state").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "参数错误2");return jsonObject.toString();}
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		//验证ID
		EntityWrapper<Orders> ew = new EntityWrapper<Orders>();
		Orders orders=ordersService.selectOne(ew.eq("merchantid", users_token.getString("id")).eq("yundanhao", yundanhao));
		
		if(orders==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "没有此数据");return jsonObject.toString();
		}
		if(state.equals(orders.getState())){
			jsonObject.put("error", "1");jsonObject.put("msg", "不能重复修改");return jsonObject.toString();
		}
		//获取修改前状态
		int aa = Integer.valueOf(orders.getState());
		
		
		//修改金额
		//获取当前余额，如果存在修改，不存在返回
		Balance balance=new Balance();
		balance=balanceService.selectOne(new EntityWrapper<Balance>().eq("usersid", orders.getMerchantid()));
		if(balance==null){
			jsonObject.put("error", "1");
			jsonObject.put("msg", "失败");
			return jsonObject.toString();
		}else{
			//判断是增加还是减少余额
			//所有状态到签收增加，从签收改为其它状态减少，其它状态money不变
			 if((!(aa==1))&Integer.valueOf(state)==1){
				BigDecimal money=UtsBigdeciArith.add(new BigDecimal(balance.getShijiyingjiekuan()),new BigDecimal(orders.getYingjiekuan()));
				balance.setShijiyingjiekuan(money+"");
			}else if(Double.valueOf(orders.getYingjiekuan())>Double.valueOf(balance.getShijiyingjiekuan())){
				jsonObject.put("error", "1");
				jsonObject.put("msg", "失败");
				return jsonObject.toString();
			}else if(aa==1&(!(Integer.valueOf(state)==1))){
				BigDecimal money=UtsBigdeciArith.sub(new BigDecimal(balance.getShijiyingjiekuan()),new BigDecimal(orders.getYingjiekuan()));
				
				balance.setShijiyingjiekuan(money+"");}
				balance.setUsersid(users_token.getString("id"));
				balance.setUsersname(users_token.getString("name"));
				balance.setUsersimg(users_token.getString("img"));
			if(!balanceService.updateById(balance)){
				jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
			}
			//执行订单表修改
			orders.setState(state);
			orders.setCzyid(users_token.getString("id"));
			orders.setCzyname(users_token.getString("name"));
			orders.setCzyimg(users_token.getString("img"));
			
			if(!ordersService.updateById(orders)){jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();}
			
			//将修改内容插入到备注
			OrdersBeizhu bz = new OrdersBeizhu();
			bz.setDdid(orders.getYundanhao());
			bz.setTid(orders.getState());
			bz.setPid("1");
			bz.setCaozuoyuanid(orders.getCzyid());
			bz.setCaozuoyuanname(orders.getCzyname());
			bz.setCaozuoyuanimg(orders.getCzyimg());
			bz.setDoc(doc);
			
			if(!ordersBeizhuService.insert(bz)){
				jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
			}
		}
		
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}	
	//客服查看订单状态
	@RequestMapping(value = { "/app/orders/kefu/state/list" })
	@ResponseBody
	public String app_orders_kefu_state_list(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();

		String token = "";
		String state = "";
		String yundanhao = "";
		String time="";
		String page = "";
		String size = "";

		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("time"))) {time = request.getParameter("time").trim();}
		if (UtsReg.getStringKB(request.getParameter("yundanhao"))) {yundanhao = request.getParameter("yundanhao").trim();}
		if(UtsReg.getInt(request.getParameter("state"))){state=request.getParameter("state").trim();}
		if (UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Orders> list = null;
		EntityWrapper<Orders> ew = new EntityWrapper<Orders>();
		if(state.length()>0){
			ew.eq("state", state);
		}
		if(time.length()>0){
			ew.like("addtime", time);
		}
		if (yundanhao.length() > 0) {
			ew.eq("yundanhao", yundanhao);
			}
		list = ordersService.selectList(ew.orderBy("id", false));
		if(list==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "没有此数据");return jsonObject.toString();
		}
		PageInfo<Orders> pages = new PageInfo<Orders>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	//客服修改订单状态
	@RequestMapping(value = { "/app/orders/kefu/upState" })
	@ResponseBody
	public String app_orders_kefu_upstate(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();
				
		//获取参数
		String token = "";
		String yundanhao="";
		String state="";
		String doc = "";
		//验证参数
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("doc"))) {doc = request.getParameter("doc").trim();} else {jsonObject.put("msg", "描述 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getIntXl(request.getParameter("yundanhao"))){yundanhao=request.getParameter("yundanhao").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "单号输入错误");return jsonObject.toString();}
		if(UtsReg.getInt(request.getParameter("state"))){state=request.getParameter("state").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "参数错误2");return jsonObject.toString();}
				
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		//验证ID
		EntityWrapper<Orders> ew = new EntityWrapper<Orders>();
		Orders orders=ordersService.selectOne(ew.eq("yundanhao", yundanhao));
		if(orders==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "没有此数据");return jsonObject.toString();
		}
		if(state.equals(orders.getState())){
			jsonObject.put("error", "1");jsonObject.put("msg", "不能重复修改");return jsonObject.toString();
		}
		//获取修改前状态
		int aa = Integer.valueOf(orders.getState());
		
		//修改金额
		//获取当前余额，如果存在修改，不存在返回
		Balance balance=new Balance();
		balance=balanceService.selectOne(new EntityWrapper<Balance>().eq("usersid", orders.getMerchantid()));
		if(balance==null){
			jsonObject.put("error", "1");
			jsonObject.put("msg", "失败");
			return jsonObject.toString();
		}else{
			//判断是增加还是减少余额
			//所有状态到签收增加，从签收改为其它状态减少，其它状态money不变
			if((!(aa==1))&Integer.valueOf(state)==1){
				BigDecimal money=UtsBigdeciArith.add(new BigDecimal(balance.getShijiyingjiekuan()),new BigDecimal(orders.getYingjiekuan()));
				balance.setShijiyingjiekuan(money+"");
			}else if(Double.valueOf(orders.getYingjiekuan())>Double.valueOf(balance.getShijiyingjiekuan())){
				jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
			}else if(aa==1&(!(Integer.valueOf(state)==1))){
				BigDecimal money=UtsBigdeciArith.sub(new BigDecimal(balance.getShijiyingjiekuan()),new BigDecimal(orders.getYingjiekuan()));
				balance.setShijiyingjiekuan(money+"");
			}
			
			if(!balanceService.updateById(balance)){
				jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
			}
		}
		
		//执行修改
		orders.setState(state);
		
		orders.setCzyid(users_token.getString("id"));
		orders.setCzyname(users_token.getString("name"));
		orders.setCzyimg(users_token.getString("img"));
		
		if(!ordersService.updateById(orders)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		//将修改内容插入到备注
		OrdersBeizhu bz = new OrdersBeizhu();
		
		bz.setDdid(orders.getYundanhao());
		bz.setTid(orders.getState());
		bz.setPid("0");
		bz.setCaozuoyuanid(orders.getCzyid());
		bz.setCaozuoyuanname(orders.getCzyname());
		bz.setCaozuoyuanimg(orders.getCzyimg());
		bz.setDoc(doc);
		
		if(!ordersBeizhuService.insert(bz)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
		
	}	
	//商户操作过的订单
	@RequestMapping(value = { "/app/orders/merchants/caozuo/list" })
	@ResponseBody
	public String app_orders_merchants_caozuo_list(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();
					
		//获取参数
		String token="";
		String yundanhao="";
		String time="";
		String page="";
		String size="";
		//验证参数
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("yundanhao"))) {yundanhao = request.getParameter("yundanhao").trim();}
		if(UtsReg.getStringKB(request.getParameter("time"))) {time = request.getParameter("time").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
				
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
			
		//验证ID
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		EntityWrapper<OrdersBeizhu> ew = new EntityWrapper<OrdersBeizhu>();
		if(yundanhao.length()>0){
			ew.eq("yundanhao", yundanhao);
		}
		if(time.length()>0){
			ew.like("addtime", time);
		}
		ew.eq("caozuoyuanid", users_token.getString("id"));
		ew.eq("pid", 1);
		List<OrdersBeizhu> beizhu = ordersBeizhuService.selectList(ew);
		PageInfo<OrdersBeizhu> pages = new PageInfo<OrdersBeizhu>(beizhu);
		
		if(beizhu==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "没有数据");return jsonObject.toString();
		}
		
		jsonObject.put("data", pages);	
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}	
	//客服操作过的订单
	@RequestMapping(value = { "/app/orders/kefu/caozuo/list" })
	@ResponseBody
	public String app_orders_kefu_caozuo_list(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();
					
		//获取参数
		String token="";
		String yundanhao="";
		String time="";
		String page="";
		String size="";
		//验证参数
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("yundanhao"))) {yundanhao = request.getParameter("yundanhao").trim();}
		if(UtsReg.getStringKB(request.getParameter("time"))) {time = request.getParameter("time").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
				
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
			
		//验证ID
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		EntityWrapper<OrdersBeizhu> ew = new EntityWrapper<OrdersBeizhu>();
		if(yundanhao.length()>0){
			ew.eq("yundanhao", yundanhao);
		}
		if(time.length()>0){
			ew.like("addtime", time);
		}
		ew.eq("caozuoyuanid", users_token.getString("id"));
		ew.eq("pid", 0);
		List<OrdersBeizhu> beizhu = ordersBeizhuService.selectList(ew);
		PageInfo<OrdersBeizhu> pages = new PageInfo<OrdersBeizhu>(beizhu);
		
		if(beizhu==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "没有数据");return jsonObject.toString();
		}
		
		jsonObject.put("data", pages);	
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}	
	//商户通过月查询月金额
	/*@RequestMapping(value = { "/app/orders/merchants/month" })
	@ResponseBody
	public String app_orders_month(HttpServletRequest request) throws MalformedURLException, IOException {

		JSONObject jsonObject = new JSONObject();
					
		//获取参数
		String token="";
		String time="";
		String page="";
		String size="";
		//验证参数
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("time"))) {time = request.getParameter("time").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
				
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
			
		//验证ID
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		EntityWrapper<Orders> ew = new EntityWrapper<Orders>();
		if(time.length()>0){ew.like("addtime", time);}
		ew.eq("merchantid", users_token.getString("id"));
		List<Orders> orders = ordersService.selectList(ew);
		if(orders==null){jsonObject.put("error", "1");jsonObject.put("msg", "没有数据");return jsonObject.toString();}
		//新建List，存查询月的所有订单应结款
		List<String> aa = new ArrayList<String>();
		for (int i = 0; i < orders.size(); i++) {
			Orders o = orders.get(i);
			aa.add(o.getYingjiekuan());
		}
		//计算sum
		Double sum = 0.0;
		for (int i = 0; i < aa.size(); i++) {
			sum+=Double.valueOf(aa.get(i));
		}
		PageInfo<Orders> pages = new PageInfo<Orders>(orders);
		
		EntityWrapper<Balance> ew1 = new EntityWrapper<Balance>();
		Balance balance = new Balance();
		ew1.eq("usersid", users_token.getString("id"));
		balance.setSum(sum+"");
		if(!balanceService.update(balance,ew1)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		jsonObject.put("data", pages);	
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}	*/
}
